package com.tssup.fesco.common.oss.object;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.tssup.fesco.common.oss.AliOssClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 获取对象
 *
 * @author shuchang.liao
 * @create 2017-05-15 11:14
 */
@Component
public class GetObject
{
	private static final int READ_BUFFER_SIZE = 1024;
	@Autowired
	private AliOssClientFactory aliOssClientFactory;
	/**
	 * 获取对象的元数据
	 * 
	 * @param bucketName
	 *            Bucket名称
	 * @param key
	 *            对象在服务器上的存储路径，可包含子文件夹名称；例如item/1.jpg
	 * @return 返回对象的元数据，如果读取失败即返回null
	 */
	public ObjectMetadata getMetadata(String bucketName, String key)
	{
		OSSClient client =aliOssClientFactory.getClient();

		try
		{
			ObjectMetadata metadata = client.getObjectMetadata(bucketName, key);

			return metadata;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(null != client)
				client.shutdown();
		}

		return null;
	}

	/**
	 * 读取字符串对象<br/>
	 * 读取指定对象并转换为String返回
	 * 
	 * @param bucketName
	 *            Bucket名称
	 * @param key
	 *            对象在服务器上的存储路径，可包含子文件夹名称；例如item/item_10001.txt
	 * @return 返回对象内容，如果获取失败即返回null
	 */
	//public String getObject(String bucketName, String key)
	//{
	//	OSSClient client = aliOssClientFactory.getClient();
    //
	//	InputStream inStream = null;
	//	ByteOutputStream outStream = null;
    //
	//	try
	//	{
	//		OSSObject object = client.getObject(bucketName, key);
	//		inStream = object.getObjectContent();
    //
	//		byte[] buffer = new byte[READ_BUFFER_SIZE];
	//		int l = -1;
	//		outStream = new ByteOutputStream();
    //
	//		while((l = inStream.read(buffer)) > 0)
	//			outStream.write(buffer, 0, l);
    //
	//		String content = new String(outStream.getBytes());
    //
	//		return content;
	//	}
	//	catch(Exception e)
	//	{
	//		e.printStackTrace();
	//	}
	//	finally
	//	{
	//		if(null != inStream)
	//		{
	//			try
	//			{
	//				inStream.close();
	//			}
	//			catch(Exception e2)
	//			{
	//			}
	//		}
    //
	//		if(null != outStream)
	//		{
	//			try
	//			{
	//				outStream.close();
	//			}
	//			catch(Exception e2)
	//			{
	//			}
	//		}
    //
	//		if(null != client)
	//			client.shutdown();
	//	}
    //
	//	return null;
	//}

	/**
	 * 获取对象的流
	 * 
	 * @param bucketName
	 *            Bucket名称
	 * @param key
	 *            对象在服务器上的存储路径，可包含子文件夹名称；例如item/1.jpg
	 * @return 返回对象流，使用完毕后必须关闭该流对象
	 */
	public InputStream getStream(String bucketName, String key)
	{
		OSSClient client = aliOssClientFactory.getClient();

		try
		{
			OSSObject object = client.getObject(bucketName, key);

			return object.getObjectContent();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(null != client)
				client.shutdown();
		}

		return null;
	}

	/**
	 * 下载指定对象到本地
	 * 
	 * @param bucketName
	 *            Bucket名称
	 * @param key
	 *            对象在服务器上的存储路径，可包含子文件夹名称；例如item/100101.jpg
	 * @param filepath
	 *            下载的文件保存在本地的路径
	 * @return 返回下载结果<br/>
	 *         true：下载成功<br/>
	 *         false：下载失败
	 */
	public boolean download(String bucketName, String key, String filepath)
	{
		OSSClient client = aliOssClientFactory.getClient();

		InputStream inStream = null;

		try
		{
			OSSObject object = client.getObject(bucketName, key);

			inStream = object.getObjectContent();

			int rc = createFile(inStream, filepath, true);

			return rc > 0;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(null != inStream)
			{
				try
				{
					inStream.close();
				}
				catch(Exception e2)
				{
				}
			}

			if(null != client)
				client.shutdown();
		}

		return false;
	}


	public int createFile(InputStream inputStream, String filePath, boolean isReplace)
	{
		File file = new File(filePath);
		if(file.isFile())
		{
			if(isReplace)
			{
				if(!file.delete())
					return -2;
			}
			else
			{
				return 2;
			}
		}

		File dir = file.getParentFile();
		if(null == dir)
			return -1;

		if(!dir.isDirectory())
		{
			try
			{
				if(!dir.mkdirs())
					return -3;
			}
			catch(Exception e)
			{
				return -3;
			}
		}

		FileOutputStream outputStream = null;

		try
		{
			if(!file.createNewFile())
				return -4;

			outputStream = new FileOutputStream(file);

			byte[] buffer = new byte[1024];
			int l = -1;
			while((l = inputStream.read(buffer)) != -1)
				outputStream.write(buffer, 0, l);

			outputStream.flush();

			return 1;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if(null != outputStream)
					outputStream.close();
			}
			catch(Exception e2)
			{
			}
		}

		return -1;
	}

}
